<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 扁平化数组

        //flat 将数组扁平化 并去除empty
        let arr = [1, 2, 3, , , [4, 5, [5, 6, 7, [8, 9]]], 10, 9]
        console.log(arr)
        let newArr1 = arr.flat(Infinity)
        console.log(newArr1)
        let newArr2 = arr.flat(1)
        console.log(newArr2)
        let newArr3 = arr.flat(0)
        console.log(newArr3)


        function myFlat(arr) {
            return arr.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? myFlat(cur) : cur), [])
        }
        /*  function myFlat(arr) {
             return arr.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? myFlat(cur) : cur), [])
         }
         function myFlat(arr) {
             return arr.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? myFlat(cur) : cur), [])
         } */

        function flatter(arr) {
            return arr.reduce((pre, cur) => Array.isArray(cur) ? [...pre, ...flatter(cur)] : [...pre, cur], [])
        }
        /*  function flatter(arr) {
             return arr.reduce((pre, cur) => Array.isArray(cur) ? [...pre, ...flatter(cur)] : [...pre, cur], [])
         }
         function flatter(arr) {
             return arr.reduce((pre, cur) => Array.isArray(cur) ? [...pre, ...flatter(cur)] : [...pre, cur], [])
         } */
        console.log(myFlat(arr))
        console.log(flatter(arr))
    </script>
</body>

</html>